var sequencingFields = "yseqKey,yseqDate,yseqState,yseqSampleCount,yseqReads,yseqProject,yseqContact,yseqBudget,yseqStart,yseqEnd,yseqNotes,yseqReadFormat,yseqRefGenome,yseqSamplePrep,yseqSamples,yseqSampleAlias,yseqInstrument,yseqAdaptorKit,yseqShearMethod,yseqMultiplexDegree,yseqKit,yseqSlider,yseqPrimer,yseqNFSSolid";

var solidNFSPath = "/proj/dudley3/sequencing/solid";

function getSequencingSearchUrl(freeSearchOp, matchingAllTerms, matchingSearchHash) {
    return "/addama-rest/dudley-sample/path/datasource/YSEQ/search?" + freeSearchOp + matchingAllTerms +
           "&PROJECTION=seqName" +
           "&PROJECTION=yseqState" +
           "&PROJECTION=sampleCount" +
           "&PROJECTION=alignmentStart" +
           "&PROJECTION=alignmentEnd" +
           "&PROJECTION=numReads" +
           "&PROJECTION=dudleyProject" +
           "&PROJECTION=budget" +
           "&PROJECTION=yseqDate" +
           "&PROJECTION=contactEmail" +
           "&PROJECTION=yseqNotes" +
           "&PROJECTION=readFormat" +
           "&PROJECTION=refGenome" +
           "&PROJECTION=samplePrep" +
           "&PROJECTION=samples" +
           "&PROJECTION=samplesAlias" +
           "&PROJECTION=instrument" +
           "&PROJECTION=adaptorKit" +
           "&PROJECTION=shearMethod" +
           "&PROJECTION=multiplexDegree" +
           "&PROJECTION=seqKit" +
           "&PROJECTION=slider" +
           "&PROJECTION=primer" +
           "&PROJECTION=nfs_solid" +
           matchingSearchHash;
}

function getSequencingSearchColumnHeaders() {
    return  [
        {
            header: 'Index',
            width: 50,
            sortable: true,
            dataIndex: 'indexNum'
        },
        {
            id:'yseqKey',
            header: 'Run',
            width: 60,
            sortable: true,
            dataIndex: 'name'
        },
        {
            header: 'Created',
            width: 70,
            sortable: true,
            dataIndex: 'dateCreated'
        },
        {
            header: 'State',
            width: 70,
            sortable: true,
            dataIndex: 'state'
        },
        {
            header: 'MultiplexDegree',
            width: 105,
            sortable: true,
            dataIndex: 'multiplexDegree'
        },
        {
            header: 'Length',
            width: 50,
            sortable: true,
            dataIndex: 'readNum'
        },
        {
            header: 'Project',
            width: 140,
            sortable: true,
            dataIndex: 'project'
        },
        {
            header: 'Contact',
            hidden: true,
            sortable: true,
            dataIndex: 'contactEmail'
        },
        {
            header: 'Budget',
            hidden: true,
            dataIndex: 'budget'
        },
        {
            header: 'AlignmentStart',
            hidden: true,
            dataIndex: 'alignmentStart'
        },
        {
            header: 'AlignmentEnd',
            hidden: true,
            dataIndex: 'alignmentEnd'
        },
        {
            header: 'yseqNotes',
            hidden: true,
            dataIndex: 'yseqNotes'
        },
        {
            header: 'readFormat',
            hidden: true,
            dataIndex: 'readFormat'
        },
        {
            header: 'refGenome',
            hidden: true,
            dataIndex: 'refGenome'
        },
        {
            header: 'samplePrep',
            hidden: true,
            dataIndex: 'samplePrep'
        },
        {
            header: 'Samples',
            width: 200,
            dataIndex: 'samples'
        },
        {
            header: 'samplesAlias',
            hidden: true,
            dataIndex: 'samplesAlias'
        },
        {
            header: 'Instrument',
            width: 80,
            dataIndex: 'instrument'
        },
        {
            header: 'adaptorKit',
            hidden: true,
            dataIndex: 'adaptorKit'
        },
        {
            header: 'shearMethod',
            hidden: true,
            dataIndex: 'shearMethod'
        },
        {
            header: 'multiplexDegree',
            hidden: true,
            dataIndex: 'multiplexDegree'
        },
        {
            header: 'seqKit',
            hidden: true,
            dataIndex: 'seqKit'
        },
        {
            header: 'slider',
            hidden: true,
            dataIndex: 'slider'
        }
    ];
}

function getSequencingPagingColumns() {
    return [
        {
            name: 'indexNum',
            type: 'float'
        },
        {
            name: 'name'
        },
        {
            name: 'dateCreated'
        },
        {
            name: 'state'
        },
        {
            name: 'sampleCount'
        },
        {
            name: 'readNum'
        },
        {
            name: 'project'
        },
        {
            name: 'contactEmail'
        },
        {
            name: 'budget'
        },
        {
            name: 'alignmentStart'
        },
        {
            name: 'alignmentEnd'
        },
        {
            name: 'yseqNotes'
        },
        {
            name: 'readFormat'
        },
        {
            name: 'refGenome'
        },
        {
            name: 'samplePrep'
        },
        {
            name: 'samples'
        },
        {
            name: 'samplesAlias'
        },
        {
            name: 'instrument'
        },
        {
            name: 'adaptorKit'
        },
        {
            name: 'shearMethod'
        },
        {
            name: 'multiplexDegree'
        },
        {
            name: 'seqKit'
        },
        {
            name: 'slider'
        },
        {
            name: 'primer'
        },
        {
            name: 'nfs_solid'
        }
    ];
}


function translateSequencingResultToJson(jsonSearchArray) {
    var jsonObj = [];
    var yseqKey = jsonSearchArray.name;
    jsonObj[0] = yseqKey.substring(4, yseqKey.length);
    jsonObj[1] = jsonSearchArray.name;
    jsonObj[2] = jsonSearchArray.yseqDate;
    jsonObj[3] = jsonSearchArray.yseqState;
    jsonObj[4] = jsonSearchArray.sampleCount;
    jsonObj[5] = jsonSearchArray.numReads;
    jsonObj[6] = jsonSearchArray.dudleyProject;
    jsonObj[7] = jsonSearchArray.contactEmail;
    jsonObj[8] = jsonSearchArray.budget;
    jsonObj[9] = jsonSearchArray.alignmentStart;
    jsonObj[10] = jsonSearchArray.alignmentEnd;
    jsonObj[11] = jsonSearchArray.yseqNotes;
    jsonObj[12] = jsonSearchArray.readFormat;
    jsonObj[13] = jsonSearchArray.refGenome;
    jsonObj[14] = jsonSearchArray.samplePrep;
    jsonObj[15] = jsonSearchArray.samples;
    jsonObj[16] = jsonSearchArray.samplesAlias;
    jsonObj[17] = jsonSearchArray.instrument;
    jsonObj[18] = jsonSearchArray.adaptorKit;
    jsonObj[19] = jsonSearchArray.shearMethod;
    jsonObj[20] = jsonSearchArray.multiplexDegree;
    jsonObj[21] = jsonSearchArray.seqKit;
    jsonObj[22] = jsonSearchArray.slider;
    jsonObj[23] = jsonSearchArray.primer;
    jsonObj[24] = jsonSearchArray.nfs_solid;
    return jsonObj;
}

function translateSeqSampleResultJsonToArray(jsonSearchArray) {
    var jsonObj = [];
    jsonObj[0] = jsonSearchArray.name;
    jsonObj[1] = jsonSearchArray.sampleStatus;
    jsonObj[2] = jsonSearchArray.sampleStatusDate;
    jsonObj[3] = jsonSearchArray.flowcell;
    jsonObj[4] = jsonSearchArray.lane;
    jsonObj[5] = jsonSearchArray.comment;
    return jsonObj;
}


function initSequencingJson(pHash) {
    pHash.seqName = undefined;
}

function getSequencingSearchOptionsArray() {
    return [
        ['all', 'All'],
        ['yseqKey', 'yseqKey'],
        ['seqName', 'seqName'],
        ['yseqDate', 'yseqDate'],
        ['contactEmail', 'contactEmail'],
        ['boxNumber', 'boxNumber'],
        ['state', 'state'],
        ['dateEntered', 'dateEntered'],
        ['yseqNotes', 'yseqNotes'],
        ['readFormat', 'readFormat'],
        ['alignmentStart', 'alignmentStart'],
        ['alignmentEnd', 'alignmentEnd'],
        ['samplePrep', 'samplePrep'],
        ['samples', 'samples'],
        ['instrument', 'instrument'],
        ['adaptorKit', 'adaptorKit'],
        ['shearMethod', 'shearMethod'],
        ['multiplexDegree', 'multiplexDegree'],
        ['seqKit', 'seqKit'],
        ['slider', 'slider'],
        ['primer', 'primer']
    ];
}


function populateSequencingJson(oHash, g) {
    oHash.name = g.getSelectionModel().getSelected().data.name;
    oHash.yseqDate = g.getSelectionModel().getSelected().data.dateCreated;
    oHash.yseqType = g.getSelectionModel().getSelected().data.type;
    oHash.yseqState = g.getSelectionModel().getSelected().data.state;
    oHash.sampleCount = g.getSelectionModel().getSelected().data.sampleCount;
    oHash.numReads = g.getSelectionModel().getSelected().data.readNum;
    oHash.dudleyProject = g.getSelectionModel().getSelected().data.project;
    oHash.budget = g.getSelectionModel().getSelected().data.budget;
    oHash.alignmentStart = g.getSelectionModel().getSelected().data.alignmentStart;
    oHash.alignmentEnd = g.getSelectionModel().getSelected().data.alignmentEnd;
    oHash.contactEmail = g.getSelectionModel().getSelected().data.contactEmail;
    oHash.yseqNotes = g.getSelectionModel().getSelected().data.yseqNotes;
    oHash.samples = g.getSelectionModel().getSelected().data.samples;
    oHash.readFormat = g.getSelectionModel().getSelected().data.readFormat;
    oHash.refGenome = g.getSelectionModel().getSelected().data.refGenome;
    oHash.samplePrep = g.getSelectionModel().getSelected().data.samplePrep;
    oHash.samples = g.getSelectionModel().getSelected().data.samples;
    oHash.samplesAlias = g.getSelectionModel().getSelected().data.samplesAlias;
    oHash.instrument = g.getSelectionModel().getSelected().data.instrument;
    oHash.adaptorKit = g.getSelectionModel().getSelected().data.adaptorKit;
    oHash.shearMethod = g.getSelectionModel().getSelected().data.shearMethod;
    oHash.multiplexDegree = g.getSelectionModel().getSelected().data.multiplexDegree;
    oHash.seqKit = g.getSelectionModel().getSelected().data.seqKit;
    oHash.slider = g.getSelectionModel().getSelected().data.slider;
    oHash.primer = g.getSelectionModel().getSelected().data.primer;
    oHash.nfs_solid = g.getSelectionModel().getSelected().data.nfs_solid;

}


function validateCreateUpdateSequencing() {

    /*if (Ext.getCmp("plasmidNameId").getValue() == null || Ext.getCmp("plasmidNameId").getValue() == "") {
     Ext.Msg.alert('Error', 'Plasmid name is required.');
     return false;
     } else if (Ext.getCmp("markersId").getValue() == null || Ext.getCmp("markersId").getValue() == "") {
     Ext.Msg.alert('Error', 'Plasmid info field is required.');
     return false;
     }else if (Ext.getCmp("yeastSelectionId").getValue() == null || Ext.getCmp("yeastSelectionId").getValue() == "") {
     Ext.Msg.alert('Error', 'Yeast selection field is required.');
     return false;
     }else if (Ext.getCmp("yeastOriginId").getValue() == null || Ext.getCmp("yeastOriginId").getValue() == "") {
     Ext.Msg.alert('Error', 'Yeast origin is required.');
     return false;
     }else if (Ext.getCmp("bacterialSelectionId").getValue() == null || Ext.getCmp("bacterialSelectionId").getValue() == "") {
     Ext.Msg.alert('Error', 'Bacterial Selection field is required.');
     return false;
     }*/

    return true;
}

/*
function popupMichellePlateLayout() {
    var pwin = new Ext.Window({
        title: 'Michelle Crossing Plate Layout',
        //style: "background: none transparent url('/addama-rest/dudley-sample/path/datasource/YSEQ/michelle-plate-layout.png') !important; padding:30px;",
        width: 600,
        height: 500,
        modal: true,
        closeAction: 'hide',
        bodyStyle: 'padding:10px;',
        html: '<img src="images/michelle-plate-layout.png" />'
    });
    pwin.show();
}

function popupVidaPlateLayout() {
    var pwin = new Ext.Window({
        title: 'Vida Crossing Plate Layout',
        //style: "background: none transparent url('/addama-rest/dudley-sample/path/datasource/YSEQ/michelle-plate-layout.png') !important; padding:30px;",
        width: 600,
        height: 500,
        modal: true,
        closeAction: 'hide',
        bodyStyle: 'padding:10px;',
        html: '<img src="images/vida-plate-layout.png"/>'
    });
    pwin.show();
}
*/

function popupPNGLayout(imageType) {
    var png = "";
    var title = "";    
    if (imageType.indexOf("RAD4") != -1){
        png = '<img src="images/rad4bp.png"/>';
        title = 'RAD 4-Based Sequence Barcode Layout';
    }else if (imageType.indexOf("VIDA") != -1){
        png = '<img src="images/vida-plate-layout.png"/>';
        title = 'Vida (YCR5) Crossing Plate Layout - YO486 x YO502';
    }else if (imageType.indexOf("MICHELLE") != -1){
        png = '<img src="images/michelle-plate-layout.png" />';
        title = 'Michelle (YCR6) Crossing Plate Layout - YO490 x YO512';
    }
    var pwin = new Ext.Window({
        title: title,
        width: 600,
        height: 500,
        modal: true,
        closeAction: 'hide',
        bodyStyle: 'padding:10px;',
        html: png
    });
    pwin.show();
}

function populateSingleSeqViaSearch(strainHash, rec) {
    /*
     jsonObj[0] = yseqKey.substring(4, yseqKey.length);
     jsonObj[1] = jsonSearchArray.name;
     jsonObj[2] = jsonSearchArray.yseqDate;
     jsonObj[3] = jsonSearchArray.yseqState;
     jsonObj[4] = jsonSearchArray.sampleCount;
     jsonObj[5] = jsonSearchArray.numReads;
     jsonObj[6] = jsonSearchArray.dudleyProject;
     jsonObj[7] = jsonSearchArray.contactEmail;
     jsonObj[8] = jsonSearchArray.budget;
     jsonObj[9] = jsonSearchArray.alignmentStart;
     jsonObj[10] = jsonSearchArray.alignmentEnd;
     jsonObj[11] = jsonSearchArray.yseqNotes;
     jsonObj[12] = jsonSearchArray.elandParam;
     jsonObj[13] = jsonSearchArray.refGenome;
     jsonObj[14] = jsonSearchArray.samplePrep;
     jsonObj[15] = jsonSearchArray.samples;
     jsonObj[16] = jsonSearchArray.samplesAlias;
     jsonObj[17] = jsonSearchArray.instrument;
     jsonObj[18] = jsonSearchArray.adaptorKit;
     jsonObj[19] = jsonSearchArray.shearMethod;
     jsonObj[20] = jsonSearchArray.multiplexDegree;
     */
    strainHash.name = rec[1];
    strainHash.yseqDate = rec[2];
    strainHash.yseqState = rec[3];
    strainHash.sampleCount = rec[4];
    strainHash.numReads = rec[5];
    strainHash.dudleyProject = rec[6];
    strainHash.contactEmail = rec[7];
    strainHash.budget = rec[8];
    strainHash.alignmentStart = rec[9];
    strainHash.alignmentEnd = rec[10];
    strainHash.yseqNotes = rec[11];
    strainHash.readFormat = rec[12];
    strainHash.refGenome = rec[13];
    strainHash.samplePrep = rec[14];
    strainHash.samples = rec[15];
    strainHash.samplesAlias = rec[16];
    strainHash.instrument = rec[17];
    strainHash.adaptorKit = rec[18];
    strainHash.shearMethod = rec[19];
    strainHash.multiplexDegree = rec[20];
    strainHash.seqKit = rec[21];
    strainHash.slider = rec[22];
    strainHash.primer = rec[23];
    strainHash.nfs_solid = rec[24];
}

function getSliderTypeCombo(hashObj) {
    var sliderStore = new Ext.data.SimpleStore({
        fields: ['key', 'value'],
        data : getSliderTypeArray()
    });
    return new Ext.form.ComboBox({
        store: sliderStore,
        id: 'yseqSlider',
        displayField:'value',
        typeAhead: true,
        mode: 'local',
        triggerAction: 'all',
        value:hashObj.slider,
        forceSelection:true,
        selectOnFocus:true
    });
}

function getSeqKitCombo(hashObj) {
    var seqKitStore = new Ext.data.SimpleStore({
        fields: ['key', 'value'],
        data : getSeqKitArray()
    });
    return new Ext.form.ComboBox({
        store: seqKitStore,
        id: 'yseqKit',
        displayField:'value',
        typeAhead: true,
        mode: 'local',
        triggerAction: 'all',
        value:hashObj.seqKit,
        forceSelection:true,
        selectOnFocus:true
    });
}


function getReadFormatCombo(hashObj) {
    var readFormatStore = new Ext.data.SimpleStore({
        fields: ['key', 'value'],
        data : [
            ['1', 'Single'],
            ['2', 'Paired end']
        ]
    });
    return new Ext.form.ComboBox({
        store: readFormatStore,
        id: 'yseqReadFormat',
        fieldLabel: 'Read format',
        displayField:'value',
        typeAhead: true,
        mode: 'local',
        triggerAction: 'all',
        value:hashObj.readFormat,
        forceSelection:true,
        selectOnFocus:true
    });
}
function getInstrumentCombo(hashObj) {
    var instrumentData = [
        ['1', 'Illumina'],
        ['2', 'SOLiD']
    ];
    var instrumentStore = new Ext.data.SimpleStore({
        fields: ['key', 'value'],
        data : instrumentData
    });
    var ic = new Ext.form.ComboBox({
        store: instrumentStore,
        id: 'yseqInstrument',
        fieldLabel: 'Instrument',
        displayField:'value',
        typeAhead: true,
        mode: 'local',
        triggerAction: 'all',
        value:hashObj.instrument,
        forceSelection:true,
        selectOnFocus:true
    });
    ic.on("select", function (c, r, i) {
        if (i == 1) {
            Ext.getCmp("solidparameters").expand();
        }
        if (i == 0) {
            Ext.getCmp("solidparameters").collapse();
        }
    });
    return ic;
}

function getPrimerCombo(hashObj) {
    var primerStore = new Ext.data.SimpleStore({
        fields: ['key', 'value'],
        data : getSeqPrimerArray()
    });
    return new Ext.form.ComboBox({
        store: primerStore,
        id: 'yseqPrimer',
        fieldLabel: 'Primer',
        displayField:'value',
        typeAhead: true,
        mode: 'local',
        triggerAction: 'all',
        value:hashObj.primer,
        forceSelection:true,
        selectOnFocus:true
    });
}

function getAdaptorKitCombo(hashObj) {
    var adaptorKitStore = new Ext.data.SimpleStore({
        fields: ['key', 'value'],
        data : getSeqAdaptorArray()
    });
    return new Ext.form.ComboBox({
        store: adaptorKitStore,
        id: 'yseqAdaptorKit',
        fieldLabel: 'Adaptor kit',
        displayField:'value',
        typeAhead: true,
        mode: 'local',
        triggerAction: 'all',
        value:hashObj.adaptorKit,
        forceSelection:true,
        selectOnFocus:true
    });
}

function getShearMethodCombo(hashObj) {
    var shearMethodStore = new Ext.data.SimpleStore({
        fields: ['key', 'value'],
        data : getSeqShearMethodArray()
    });
    return new Ext.form.ComboBox({
        store: shearMethodStore,
        id: 'yseqShearMethod',
        fieldLabel: 'Fragmentation',
        displayField:'value',
        typeAhead: true,
        mode: 'local',
        triggerAction: 'all',
        value:hashObj.shearMethod,
        forceSelection:true,
        selectOnFocus:true
    });
}

function getRefGenomeCombo(hashObj) {
  var refGenomeStore = new Ext.data.SimpleStore({
        fields: ['key', 'value'],
        data : getSeqRegGenomeArray()
    });
    return new Ext.form.ComboBox({
        store: refGenomeStore,
        id: 'yseqRefGenome',
        fieldLabel: 'Ref. genome',
        displayField:'key',
        typeAhead: true,
        mode: 'local',
        triggerAction: 'all',
        value:hashObj.refGenome,
        forceSelection:true,
        selectOnFocus:true
    });
}
